################################################################################
# Judicial Ideology as Text -- Figure 1: Static Ideal Points by Weight
# Truscott & Romano
# Updated February 2025
################################################################################


################################################################################
# Load Source
################################################################################

source('R/truscott_romano_source.R') # Load Source

################################################################################
# Figure 1: Static Ideal Points by Weight
################################################################################

combined_wordshoal <- data.frame()

for (i in 1:length(wordshoals_static)){

  combined_wordshoal <- bind_rows(combined_wordshoal,
                                  wordshoals_static$Full$summary_output %>% mutate(type = names(wordshoals_static[i])))


}

{

  figure_1 <- combined_wordshoal %>%
    filter(type == 'High') %>%
    rename(justice = author) %>%
    mutate(justice = ifelse(justice == 'Connor',"O'Connor", justice),
           ideology = ifelse(justice %in% c('Breyer', 'Ginsburg', 'Sotomayor', 'Kagan'), 'Democratic', 'Republican'),
           type = paste0(type, ' Weight'),
           ideology = ifelse(ideology == 'Democratic', 'Democratic Appointee', 'Republican Appointee'),
           ideology = factor(ideology, levels = c('Democratic Appointee', 'Republican Appointee')),
           lower = theta - 1.96 * se,
           upper = theta + 1.96 * se) %>%
    ggplot(aes(x = theta, y = reorder(justice, theta), group = factor(type), colour = ideology, linetype = ideology, shape = ideology)) +
    geom_errorbar(aes(xmin = lower, xmax = upper), width = 0.15, linewidth = 1, position = position_dodge(width = 1)) +
    geom_point(size = 2, position = position_dodge(width = 1), aes(fill = ideology), colour = 'black') +
    scale_shape_manual(values = c(21, 24)) +
    #facet_wrap(~factor(type)) +
    theme_minimal() +
    geom_vline(xintercept = 0, linetype = 2, alpha = 1/5) +
    scale_x_continuous(breaks = seq(-4, 4, 2), lim = c(-5, 5)) +
    scale_colour_manual(values = c('black', 'grey50')) +
    scale_fill_manual(values = c('black', 'grey50')) +
    labs(
      x = expression("\nIdeal Point" ~ italic(Theta) ~ '\n'),
      y = ' ',
      fill = 'Party of Appointing President',
      colour = 'Party of Appointing President',
      linetype = 'Party of Appointing President',
      shape = 'Party of Appointing President') +
    theme(
      axis.title = element_text(size = 14),
      axis.text = element_text(size = 12, colour = 'gray5'),
      panel.border = element_rect(linewidth = 1, color = "gray5", fill = NA),
      legend.title.align = 0.5,
      legend.text = element_text(size = 12, color = "gray5"),
      legend.position = "bottom",
      strip.text = element_text(size = 14, face = "bold"),
      strip.background = element_rect(fill = "gray", color = "gray5"),
      plot.title = element_text(size = 18, face = "bold", hjust = 0.5),
      plot.subtitle = element_text(size = 15, hjust = 0.5),
      plot.caption = element_text(size = 12, hjust = 0, colour = 'gray5'),
      legend.title = element_blank(),
      legend.box.background = element_rect(size = 1, color = 'gray5', fill = NA))

  ggsave(figure_1,
         file = 'figures/Figure_1.png',
         width = 10,
         height = 8,
         units = 'in',
         bg = 'white')

  ggsave('figures_tiff/Figure_1.tiff',
         plot = figure_1,
         device = 'tiff',
         dpi = 600,
         width = 10,
         height = 10,
         units = 'in',
         bg = 'white')




} # Static Ideal Points by Weight (Figure 1)
